package com.lzit.smart_canteen.controller;

import com.lzit.smart_canteen.domain.CartItem;
import com.lzit.smart_canteen.domain.Food;
import com.lzit.smart_canteen.service.FoodService;
import com.lzit.smart_canteen.service.ItemService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;

@Controller
public class ItemController {
    @Autowired
    FoodService foodService;
    @Autowired
    ItemService itemService;
    //向Cart添加Item
    @RequestMapping("/addItem")
    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int fid = Integer.parseInt(req.getParameter("foodId"));
        int uid = Integer.parseInt(req.getParameter("userId"));
        Food food = foodService.queryFoodById(fid);
        CartItem item = itemService.findCartByUidAndFid(uid,fid);
        //如果这个Item存在，就增加数量
        if (item != null){
            Integer count = item.getCount() + 1;
            item.setCount(count);
            itemService.updateCartItem(item.getUid(),item.getFid(), count);
        } else {
            //否则重新创建这个Item
            Integer userId = Integer.valueOf(req.getParameter("userId"));
            // 把信息，转换成为CartItem商品项
            CartItem cartItem = new CartItem();
            cartItem.setFname(food.getName());
            cartItem.setWindow(food.getWindow());
            cartItem.setPrice(food.getPrice());
            cartItem.setCount(1);
            cartItem.setFid(food.getId());
            cartItem.setUid(userId);
            //获取sql格式的时间
//        java.sql.Date date = new Date(Calendar.getInstance().getTime().getTime());
            System.out.println(cartItem);
            itemService.addItem(cartItem);

        }
    }
    //购物车Item数量增
    @RequestMapping("/addCount")
    protected void addCount(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        int fid = Integer.parseInt(req.getParameter("foodId"));
        int uid = Integer.parseInt(req.getParameter("userId"));
        CartItem item = itemService.findCartByUidAndFid(uid,fid);
        if (item.getCount() >= 1){
            Integer count = item.getCount() + 1;
            item.setCount(count);
            itemService.updateCartItem(item.getUid(),item.getFid(), count);
        }
    }
    //购物车Item数量减
    @RequestMapping("/reduceCount")
    protected void reduceCount(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        int fid = Integer.parseInt(req.getParameter("foodId"));
        int uid = Integer.parseInt(req.getParameter("userId"));
        CartItem item = itemService.findCartByUidAndFid(uid,fid);
        if (item.getCount() > 1){
            Integer count = item.getCount() - 1;
            item.setCount(count);
            itemService.updateCartItem(item.getUid(),item.getFid(), count);
        } else {
            itemService.deleteCartItemByUidAndFid(uid,fid);
        }
    }
}
